<?php
/**
 * Created by chency.
 * User: Administrator
 * Date: 2018/11/4 0004
 * Time: 下午 6:12
 */
namespace app\admin\model;

use think\cache\driver\Redis;
use think\Db;
use think\Model;

class SceneryModel extends Model
{
    public $table = 'tps_scenery';

    private $cacheListName = 'SCENERY_CACHE_LIST';

    private $cacheDetailName = 'SCENERY_CACHE_DETAIL';

    /**
     * 获取列表
     * @param $map
     * @param $page
     * @param int $listRows
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getList ($map, $page, $listRows = 5)
    {
        $list = $this
            ->field ('s.id,s.name,s.price,s.address,s.address_latitude,s.address_longitude,s.label,s.url,p.url AS pic_url,s.created_at,s.updated_at,s.status')
            ->alias ('s')
            ->join ('tps_picture p', 'p.id = s.pic_id', 'left')
            ->where ($map)
            ->order ('s.created_at DESC')
            ->paginate ($listRows);
        foreach ($list AS &$one) {
            $one['label'] = explode ('|', $one['label']);
        }
        return $list;
    }

    /**
     * 获取单条
     * @param $map
     * @return array|false|\PDOStatement|string|Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function getDetails ($map)
    {
        $redis = new Redis();
        $redis->rm ($this->cacheListName);
        $redis->rm ($this->cacheDetailName);
        $detail = $this
            ->field ('s.id,s.name,s.price,s.address,s.address_latitude,s.address_longitude,s.label,s.url,p.url AS pic_url,ni.content AS introduce,s.traffic_id,s.introduce_id,nt.content AS traffic,s.created_at,s.updated_at,s.status')
            ->alias ('s')
            ->join ('tps_picture p', 'p.id = s.pic_id', 'left')
            ->join ('tps_news ni', 'ni.id = s.introduce_id', 'left')
            ->join ('tps_news nt', 'nt.id = s.traffic_id', 'left')
            ->where ($map)
            ->find ();
        $detail['label'] = explode ('|', $detail['label']);
        //获取上一条 和下一条
        $detail['next_id'] = $this->where (['created_at' => ['lt', $detail['created_at']]])->order ('created_at DESC')->value ('id');
        $detail['pre_id'] = $this->where (['created_at' => ['gt', $detail['created_at']]])->order ('created_at ASC')->value ('id');
        return $detail;
    }

    /**
     * 获取景点新闻
     * @param $map
     * @param $page
     * @param int $listRows
     * @return \think\Paginator
     */
    public function getNewsList ($map, $listRows = 5)
    {
        $list = Db::table ('tps_scenery_news')
            ->field ('n.id,n.type,n.title,n.title_source,n.pic,n.url,n.description,n.author,n.status,n.created_at,n.updated_at,nt.type_name')
            ->alias ('s')
            ->join ('tps_news n', 'n.id = s.news_id AND s.status = 1', 'left')
            ->join ('tps_news_type nt', 'nt.id = n.type', 'left')
            ->where ($map)
            ->paginate ($listRows);
        return $list;
    }
}